export function createEventHook<T = any>() {
  const fns = new Set<(value: T) => void>()

  const off = (handler: (value: T) => void) => {
    fns.delete(handler)
  }

  const clear = () => {
    fns.clear()
  }

  const on = (handler: (value: T) => void) => {
    fns.add(handler)

    const offFn = () => off(handler)

    return offFn
  }

  const trigger = (value: T) => {
    fns.forEach((handler) => handler(value))
  }

  return {
    on,
    off,
    clear,
    trigger,
  }
}
